package utils

import (
	"errors"
	validation "github.com/go-ozzo/ozzo-validation/v3"
	"github.com/go-ozzo/ozzo-validation/v3/is"
	"reflect"
	"regexp"
	"strings"
)

//type ValidationType struct {
//	Required bool //是否为必填
//	Length [2]int //字节长度限制
//	Match string //正则验证 需要填写正则的字符串
//	Email bool //判断是否为email
//	RuneLength [2]int //字符串长度限制
//}

func ValidateData(data interface{}, rules ...*validation.FieldRules) (err error) {
	err = validation.ValidateStruct(data, rules...)
	if err != nil {
		err = formattingError(err)
	}
	return
}

func ValidateFields(str interface{}, rules ...validation.Rule) *validation.FieldRules {
	return validation.Field(str, rules...)
}

func ValidateOneField(str interface{}, rules ...validation.Rule) (err error) {
	return validation.Validate(str, rules...)
}

//必填
func Required(err string) validation.Rule {
	rule := validation.Required
	if err != "" {
		rule = rule.Error(err)
	}
	return rule
}

//字节长度
func Length(min int, max int, err string) validation.Rule {
	rule := validation.Length(min, max)
	if err != "" {
		rule = rule.Error(err)
	}
	return rule
}

//字符串长度
func RuneLength(min int, max int, err string) validation.Rule {
	rule := validation.RuneLength(min, max)
	if err != "" {
		rule = rule.Error(err)
	}
	return rule
}

//最小值
func Min(min int, err string) validation.Rule {
	rule := validation.Min(min)
	if err != "" {
		rule = rule.Error(err)
	}
	return rule
}

//最大值
func Max(max int, err string) validation.Rule {
	rule := validation.Max(max)
	if err != "" {
		rule = rule.Error(err)
	}
	return rule
}

//正则 验证规则
func Match(str string, err string) validation.Rule {
	rule := validation.Match(regexp.MustCompile(str))
	if err != "" {
		rule = rule.Error(err)
	}
	return rule
}

//in 特殊处理
func In(err string, data ...interface{}) validation.Rule {
	rule := validation.In(data...)
	if err != "" {
		rule = rule.Error(err)
	}
	return rule
}

func IsEmail(err string) validation.Rule {
	rule := is.Email
	if err != "" {
		rule = rule.Error(err)
	}
	return rule
}

func IsURL(err string) validation.Rule {
	rule := is.URL
	if err != "" {
		rule = rule.Error(err)
	}
	return rule
}

func IsRequestURL(err string) validation.Rule {
	rule := is.RequestURL
	if err != "" {
		rule = rule.Error(err)
	}
	return rule
}

func IsRequestURI(err string) validation.Rule {
	rule := is.RequestURI
	if err != "" {
		rule = rule.Error(err)
	}
	return rule
}

func IsInt(err string) validation.Rule {
	rule := is.Int
	if err != "" {
		rule = rule.Error(err)
	}
	return rule
}

func IsFloat(err string) validation.Rule {
	rule := is.Float
	if err != "" {
		rule = rule.Error(err)
	}
	return rule
}

func IsJSON(err string) validation.Rule {
	rule := is.JSON
	if err != "" {
		rule = rule.Error(err)
	}
	return rule
}

//验证字符串是否为有效域
func IsDomain(err string) validation.Rule {
	rule := is.Domain
	if err != "" {
		rule = rule.Error(err)
	}
	return rule
}

//自定义函数
func Custom(f validation.RuleFunc) validation.Rule {
	return validation.By(f)
}

func formattingError(err error) error {
	arr := strings.Split(err.Error(), ";")
	var errStr string
	for _, v := range arr {
		split := strings.Split(v, " ")
		errStr = reflect.ValueOf(split).Index(1).String()
		return errors.New(errStr)
	}
	return nil
}
